package webserver.bank;

import java.io.IOException;

import std.bank.Account;
import std.bank.Bank;
import webserver.standard.Server;
import webserver.standard.WebServer;

public class JGroupMain
{

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException
	{
		if (args.length >= 3)
		{
			if (args[0].startsWith("client"))
			{
				System.out.println("Starts client");

				ClientStubSetting.setClientPort(Integer.parseInt(args[2]));
				ClientStubSetting.setHost(args[1]);
				ClientStubSetting.setProtocolName("bank");
				Bank b = new BankRPC();

				Account allan = b.getAccount("allan");
				Account bjarke = b.getAccount("bjarke");
				Account milo = b.getAccount("Daniel");
				bjarke.deposit(1000); // Bjarke solgte sin sjael
				System.out.println(b.getAccount("bjarke"));
				allan.deposit(10);
				bjarke.withdraw(10); // Bjarke koebte Britney Spears
				milo.withdraw(10); // Milo gav to oel som han ikke have raad
				// til!
				System.out.println(b.getAccount("bjarke"));
				System.out.println(b.getAccount("allan"));
				System.out.println(b.getAccount("Daniel"));
				Account a = new AccountRPC("none", 0);
				System.out.println(a);
				a.withdraw(1000);
				System.out.println(a);
			}
			else if (args[0].startsWith("server"))
			{
				System.out.println("Starts server");
				Server server = new WebServer(Integer.parseInt(args[2]),
						new RPCHandlerFactory(), "/");
				server.attachHandlers("bank/", new RPCHandlerFactory());
				server.joinJGroupChannel("properties.xml", "AwesomeBank");
				server.forwardRequest();
				System.out.println("Server started");
			}
		}
		else
		{
			System.out.println("To few parameters");
		}
	}

}
